<?php
/*
 * @Description: 企业微信相关SDK
 * @Version: 1.0
 * @Author: beiwangshan
 * @Date: 2023-02-17 20:09:20
 * @LastEditors: beiwangshan
 * @LastEditTime: 2023-03-04 17:43:13
 */
class ZEO_QYWX
{
    static function RefreshAccessToken($corpid, $corpsecret)
    {
        $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$corpid}&corpsecret={$corpsecret}&debug=1";
        $data = json_decode(EL_HTTP::doGet($url))->body;
        $access_token_data = $data->access_token;
        $access_token = array();
        $access_token['access_token'] = $access_token_data;
        $access_token['expires'] = time() + 7000;

        // 将数组写入 php 文件
        $arr = '<?php' . PHP_EOL . '$access_token = ' . var_export($access_token, true) . ';' . PHP_EOL . '?>';
        $arrfile = fopen(ZEO_THEME_DIR .'/wx_token.php', "w");
        fwrite($arrfile, $arr);
        fclose($arrfile);
        // 返回当前的 access_token
        return $access_token_data;
    }


    static function getQywxToken($corpid, $corpsecret)
    {

        include ZEO_THEME_DIR .'/wx_token.php';
        $expires = $access_token['expires'];
        // 判断是否过期
        if ($access_token['access_token'] == NULL || time() > $expires) {
            // 返回当前的 access_token
            return self::RefreshAccessToken($corpid, $corpsecret);
        } else {
            // 如果没有过期就直接读取缓存文件
            return $access_token['access_token'];
        }
    }
    // 发送应用消息函数
    static function send($corpid, $corpsecret, $postData)
    {
        $access_token = self::getQywxToken($corpid, $corpsecret);
        $postUrl = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' . $access_token;
        $data = EL_HTTP::doPost($postUrl, $postData);
        return $data;
    }

    /**
     * 替换模板消息
     */
    static function  replaceTextTemplate($template, $post_ID)
    {
        if ($post_ID) {
            $post = json_decode(zeo_get_post_info($post_ID));
        }
        if (!($post->post_status == 'publish' && $_POST['original_post_status'] != 'publish')) {
            return;
        }
        //定义可以替换的模版字段
        $template = str_replace('[title]', $post->post_title, $template);
        $template = str_replace('[date]', $post->post_date, $template);
        $template = str_replace('[author]', $post->post_author, $template);
        $template = str_replace('[desc]', zeo_get_post_excerpt($post, 50), $template);
        $template = str_replace('[url]', $post->url, $template);
        //返回替换后的值
        $text = array(
            'content' => $template
        );
        return $text;
    }
    static function  replaceTextCardTemplate($post_ID)
    {
        if ($post_ID) {
            $post = json_decode(zeo_get_post_info($post_ID));
        }

        if ($post->post_status == 'publish' && $_POST['original_post_status'] != 'publish') {
            $html = '<div class="gray">' . $post->post_date . '</div> <br><div class="normal">' . zeo_get_post_excerpt($post, 50) . '</div> <br><div class="highlight">更多内容，请点击详情查看</div>';        //定义可以替换的模版字段
            $textcard = array(
                'title' => $post->post_title,
                'description' => $html,
                'url' => $post->url,
                'btntxt' => '详情'
            );
        }
        return $textcard;
    }
    static function  replaceNewsTemplate($post_ID)
    {
        if ($post_ID) {
            $post = json_decode(zeo_get_post_info($post_ID));
        }
        if (!($post->post_status == 'publish' && $_POST['original_post_status'] != 'publish')) {
            return;
        }
        $news = array(
            'articles' => array(
                'title' => $post->post_title,
                'description' => '您关注的网站有内容更新啦！',
                'url' => $post->url,
                'picurl' => $post ->cover,
            ),
        );
        //返回替换后的值
        return $news;
    }
}
